/**
 * Created by Administrator on 2017/5/13.
 * 获取数据列表的混合 包含 日期过滤、数据获取、搜索
 *
 */
import moment from "moment"
import {mapActions} from 'vuex'
export default {
      data() {
          return {
              dateRange: [],
              searchObj: {
                  pageSize: 10
              },
              totalRows: 0,
              tableData: []
          }
    },
    created () {
        const notInit = this.$options.notInit;
        if (!notInit) {
            this.getList(this.url);
        }
    },
    methods: {
        ...mapActions(['setLoadingText']),
        dateChange () {
            if (typeof this.dateRange[0] !== 'undefined' && typeof this.dateRange[1] !== 'undefined') {
                this.searchObj.StartDate = moment(this.dateRange[0]).format('YYYYMMDD');
                this.searchObj.EndDate = moment(this.dateRange[1]).format('YYYYMMDD');
            } else {
                this.searchObj.StartDate = '';
                this.searchObj.EndDate = '';
            }
        },
        search(){
            this.getList(this.url, 1);
            return false;
        },
        //分页变化时
        handleCurrentChange (page) {
            this.getList(this.url, page);
        },
        //pageSize 变化时
        handlePageSizeChange (pageSize){
            this.searchObj.pageSize = pageSize;
            this.getList(this.url, 1);
        },
        /**
         * switch开关请求
         *
         * @param [string] url
         * @param [object] data
         *
         * */
        switchChangePost ({url, data}) {
            this.$http.post(url, data)
            .then(res => {
                const msg = res.body
                if (msg.Success) {
                    this.$message.success({message: '操作成功!'});
                } else {
                    this.$message.error({message: msg.Msg});
                }
            }, res => {
                this.$message.error({message: res.statusText});
            })
        },

        /**
         * 删除行
         *
         * @param [string] url
         * @param [object] params
         * @param [string] title
         *
         * */
        deleteRow ({title, url, params, cb}) {
            this.$confirm(title, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.setLoadingText('正在删除...');
                this.$http.delete(url, {params: params}).then(res => {
                    const msg = res.body;
                    if (msg.Success) {
                        this.$message.success('删除成功！');
                        this.getList(this.url, this.searchObj.pageIndex);
                    } else {
                        this.$message.error({message: '删除失败!'});
                    }
                })
            }).catch(() => {
            })
        },
        //获取列表的请求方法，接收请求地址url和请求页码两个参数
        getList (url, page) {
            this.searchObj.pageIndex = page || 1;
            this.$http.post(url, {obj: JSON.stringify(this.searchObj)}, {emulateJSON: true})
            .then(res => {
                const msg = res.body;
                if (msg.Success) {
                    this.tableData = msg.Data;
                    this.totalRows = msg.Total;
                }else{
                  this.$message.error({message: '数据获取失败!'});
                }
            }, res => {
                //this.$message.error({message: '数据获取失败!'});
            })
        }

    }
}
